package cn.iocoder.yudao.module.alipay.service.elong.impl;

import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.common.JsonResult;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.common.PagerDateResult;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.common.ResultInfo;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.hotel.HotelDetailForListPager;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.hotel.HotelDetailRequest;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.hotel.HotelDetailViewModel;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.hotel.ListPagerRequest;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.order.CreateOrderRequest;
import cn.iocoder.yudao.module.alipay.dal.dataobject.elong.pay.PaySubmitRequest;
import cn.iocoder.yudao.module.alipay.service.elong.ElongHotalService;
import cn.iocoder.yudao.module.alipay.service.elong.HotelService;
import cn.iocoder.yudao.module.alipay.service.elong.PayService;
import cn.iocoder.yudao.module.alipay.service.elong.ValidateService;
import cn.iocoder.yudao.module.alipay.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.PARAMS_ERROR;

@Service
@Slf4j
public class ElongHotalServiceImpl implements ElongHotalService {


    @Resource
    private HotelService hotelService;

    @Resource
    private ValidateService validateService;

    @Resource
    private PayService payService;

    @Override
    public JsonResult<PagerDateResult<HotelDetailForListPager>> getHotelList(ListPagerRequest pagerReq, HttpServletRequest request) {
        JsonResult<PagerDateResult<HotelDetailForListPager>> result = new JsonResult<PagerDateResult<HotelDetailForListPager>>();
        try {
            //参数验证与处理
            if (pagerReq == null) {
                pagerReq = new ListPagerRequest();
            }
            if (pagerReq.getArrivalDate() == null) {
                pagerReq.setArrivalDate(new Date());
            }
            if (pagerReq.getDepartureDate() == null) {
                pagerReq.setDepartureDate(DateUtil.addDate(new Date(), 1));
            }
            if (pagerReq.getCityId() == null || "".equals(pagerReq.getCityId())) {
                pagerReq.setCityId("0101");//默认设置为北京
            }
            if (pagerReq.getPageIndex() == null || pagerReq.getPageIndex() <= 0) {
                pagerReq.setPageIndex(1);//默认设置为第一页
            }
            if (pagerReq.getArrivalDate().getTime() >= pagerReq.getDepartureDate().getTime()) {
                result.setMsg("离店日期必须大于入店日期");
                return result;
            }
            Date nowDay = DateUtil.getDateTimeFormat(DateUtil.getCurrentDateStr("yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd");
            if (pagerReq.getArrivalDate().getTime() < DateUtil.addDate(nowDay, -1).getTime()) {
                result.setMsg("入店日期必须大于等于昨天");
                return result;
            }
            if (DateUtil.getDifferenceBetweenDay(pagerReq.getDepartureDate(), pagerReq.getArrivalDate()) > 20) {
                result.setMsg("入离店时间距离不能大于20天");
                return result;
            }

            //请求业务层获取数据
            ResultInfo<PagerDateResult<HotelDetailForListPager>> hotelListRet = hotelService.getHotelList(pagerReq);
            if (hotelListRet.getIsSuccess()) {
                result.setData(hotelListRet.getData());
                result.setState(true);
                result.setMsg("获取成功");
            } else {
                result.setMsg("获取数据失败：" + hotelListRet.getMsg());
                System.out.print(hotelListRet.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询异常");
        }
        return result;
    }

    @Override
    public HotelDetailViewModel getHotelDetail(HotelDetailRequest detailReq, HttpServletRequest request, HttpServletResponse response) {
        //参数判断
        if (detailReq == null
                || detailReq.getHotelId() == null
                || "".equals(detailReq.getHotelId().trim())
                || detailReq.getArrivalDate() == null
                || detailReq.getDepartureDate() == null
                || detailReq.getArrivalDate().getTime() > detailReq.getDepartureDate().getTime()) {
            throw exception(PARAMS_ERROR);
        }
        //数据查询
        ResultInfo<HotelDetailViewModel> detailRet = hotelService.getHotelDetail(detailReq.getHotelId(), detailReq.getArrivalDate(), detailReq.getDepartureDate());

        if (detailRet.getIsSuccess()) {
            HotelDetailViewModel data = detailRet.getData();
            return data;
        } else {
            System.out.print(detailRet.getMsg());
            return null;
        }
    }

    @Override
    public Map<String, Object> createdOrder(CreateOrderRequest orderRequest, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> resultMap = new HashMap<>();
        //参数验证
        if (orderRequest == null
                || orderRequest.getArrivalDate() == null
                || orderRequest.getDepartureDate() == null
                || orderRequest.getHotelCode() == null
                || "".equals(orderRequest.getHotelCode().trim())
                || orderRequest.getHotelId() == null
                || "".equals(orderRequest.getHotelId().trim())
                || orderRequest.getRoomTypeId() == null
                || "".equals(orderRequest.getRoomTypeId().trim())
                || orderRequest.getRatePlanId() <= 0
                || orderRequest.getArrivalDate().getTime() > orderRequest.getDepartureDate().getTime()) {
            throw exception(PARAMS_ERROR);
        }
        //查询酒店详情
        ResultInfo<HotelDetailViewModel> detailRet = hotelService.getHotelDetail(orderRequest.getHotelId(),
                orderRequest.getArrivalDate(), orderRequest.getDepartureDate(), orderRequest.getRatePlanId(), orderRequest.getRoomTypeId());
        if (detailRet.getIsSuccess()) {
            HotelDetailViewModel data = detailRet.getData();
            resultMap.put("detail", data);
        } else {
            resultMap.put("errorMsg", "查询酒店详情失败");
            System.out.print(detailRet.getMsg());
            return resultMap;
        }
        //进行库存验证
        ResultInfo<Boolean> validateRet = validateService.validateData(orderRequest);
        if (validateRet.getIsSuccess() == false) {
            resultMap.put("errorMsg", validateRet.getMsg());
            return resultMap;
        }
        return resultMap;
    }

    @Override
    public JsonResult<Boolean> submitOrder(PaySubmitRequest paySubmitReq, HttpServletRequest request) {
        JsonResult<Boolean> result = new JsonResult<Boolean>();
        //参数验证
        if (paySubmitReq == null
                || paySubmitReq.getCardNo() == null
                || "".equals(paySubmitReq.getCardNo().trim())
                || paySubmitReq.getExpiration() == null
                || "".equals(paySubmitReq.getExpiration().trim())
                || paySubmitReq.getExpiration().trim().length() != 6
                || paySubmitReq.getHolderName() == null
                || "".equals(paySubmitReq.getHolderName().trim())
                || paySubmitReq.getIdNo() == null
                || "".equals(paySubmitReq.getIdNo().trim())
                || paySubmitReq.getIdType() == null
                || paySubmitReq.getOrderId() == 0
                || paySubmitReq.getCardNo().length() > 20
                || paySubmitReq.getHolderName().length() > 20
                || paySubmitReq.getIdNo().length() > 20) {
            throw exception(PARAMS_ERROR);
        }
        //支付请求
        ResultInfo<Boolean> payRet = payService.submitOrderPay(paySubmitReq);
        result.setMsg(payRet.getMsg());
        result.setState(payRet.getIsSuccess());
        result.setMsg("支付请求异常");
        return result;
}
}
